package com.stormrage.edu.dao.adm.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.stormrage.edu.dao.adm.AdmAcctDao;
import com.stormrage.entity.adm.AdmAcct;
import com.stormragetech.core.dao.MysBaseDao;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @author www.stormragetech.com
 * @date 2016年1月19日
 * @version 1.0.0
 */
@Repository
public class AcctDaoImpl extends MysBaseDao implements AdmAcctDao {

	private static final Logger logger = LoggerFactory.getLogger(AcctDaoImpl.class);

	@Override
	public Integer create(final AdmAcct arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 AdmAcctDao 】- 【 Create 】- 开始运行 ");

		final String query = "INSERT INTO adm_acct (acct, pass, salt, role, real_name, locked) VALUES"
				+ " (:acct, :pass, :salt, :role, :realName, :locked)";
		logger.debug(" 数据库执行语句为 " + query);

		Integer index = create(query, arg0);

		logger.debug(" 【 Dao 】- 【 AdmAcctDao 】- 【 Create 】- 完成运行 ");
		return index;
	}

	@Override
	public void update(final AdmAcct arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 AdmAcctDao 】- 【 Update 】- 开始运行 ");

		final String query = "UPDATE adm_acct SET real_name = :realName WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);
		logger.debug(" 【 Dao 】- 【 AdmAcctDao 】- 【 Update 】- 完成运行 ");
	}

	@Override
	public void updatePass(final AdmAcct arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 AdmAcctDao 】- 【 UpdatePass 】- 开始运行 ");

		final String query = "UPDATE adm_acct SET pass = :pass, salt = :salt WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);
	}

	@Override
	public void updateLocked(final AdmAcct arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 AdmAcctDao 】- 【 updateLocked 】- 开始运行 ");

		final String query = "UPDATE adm_acct SET locked = :locked WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);
	}

	@Override
	public AdmAcct queryById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 AdmAcctDao 】- 【 QueryById 】- 开始运行 ");

		final String query = "SELECT * FROM adm_acct WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		AdmAcct entity = queryObject(query, AdmAcct.class, map);
		logger.debug(" 查询结果为 " + entity);

		return entity;
	}

	@Override
	public AdmAcct queryByIdLock(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 AdmAcctDao 】- 【 QueryByIdLock 】- 开始运行 ");

		final String query = "SELECT * FROM adm_acct WHERE id = :id AND locked = :locked";
		logger.debug(" 数据库执行语句为 " + query);

		AdmAcct entity = queryObject(query, AdmAcct.class, map);
		logger.debug(" 查询结果为 " + entity);

		return entity;
	}

	@Override
	public AdmAcct queryByAcct(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 AdmAcctDao 】- 【 QueryByAcct 】- 开始运行 ");

		final String query = "SELECT * FROM adm_acct WHERE acct = :acct";
		logger.debug(" 数据库执行语句为 " + query);

		AdmAcct entity = queryObject(query, AdmAcct.class, map);
		logger.debug(" 查询结果为 " + entity);

		return entity;
	}

	@Override
	public AdmAcct queryByAcctLock(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 AdmAcctDao 】- 【 QueryByAcctLock 】- 开始运行 ");

		final String query = "SELECT * FROM adm_acct WHERE acct = :acct AND locked = :locked";
		logger.debug(" 数据库执行语句为 " + query);

		AdmAcct entity = queryObject(query, AdmAcct.class, map);
		logger.debug(" 查询结果为 " + entity);

		return entity;
	}

	@Override
	public List<AdmAcct> queryList() throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 AdmAcctDao 】- 【 queryList 】- 开始运行 ");

		final String query = "SELECT * FROM adm_acct";
		logger.debug(" 数据库执行语句为 " + query);

		List<AdmAcct> array = queryList(query, AdmAcct.class);
		logger.debug(" 查询结果为 " + array);

		return array;
	}

}
